package com.example.yichuangyun.mvvm.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.DataBindingHolder;
import com.example.yichuangyun.R;
import com.example.yichuangyun.databinding.ItemLabelBinding;
import com.example.yichuangyun.model.business.LabelModel;

import java.util.List;

@SuppressLint("NotifyDataSetChanged")
public class OrderLabelAdapter extends BaseQuickAdapter<LabelModel, DataBindingHolder<ItemLabelBinding>> {

    private final OnLabelItemClickListener onLabelItemClickListener;
    private final Context context;
    private final List<LabelModel> mData;
    private final int flag;

    public OrderLabelAdapter(Context context, List<LabelModel> mData, int flag, OnLabelItemClickListener onLabelItemClickListener) {
        this.context = context;
        this.mData = mData;
        this.flag = flag;
        this.onLabelItemClickListener = onLabelItemClickListener;
    }

    public void resetData(int flag) {
        for (LabelModel labelModel : mData) {
            labelModel.setSelect(false);
        }
        if (flag == 1) {
            mData.get(0).setSelect(true);
        }
        notifyDataSetChanged();
    }

    @Override
    protected void onBindViewHolder(@NonNull DataBindingHolder<ItemLabelBinding> itemLabelBindingDataBindingHolder, int i, @Nullable LabelModel labelModel) {
        ItemLabelBinding binding = itemLabelBindingDataBindingHolder.getBinding();
        if (labelModel != null) {
            binding.tvLabel.setText(labelModel.getRemark());
            if (labelModel.isSelect()) {
                binding.tvLabel.setTextColor(0xFF426FFF);
                binding.tvLabel.setBackgroundResource(R.drawable.shape_time_bg_sel);
            } else {
                binding.tvLabel.setTextColor(0xFF4A4A4A);
                binding.tvLabel.setBackgroundResource(R.drawable.shape_label_bg_def);
            }
            binding.tvLabel.setOnClickListener(v -> {
                if (flag == 1) {
                    if (!labelModel.isSelect()) {
                        for (LabelModel model : mData) {
                            model.setSelect(false);
                        }
                        labelModel.setSelect(true);
                    }
                } else {
                    if (labelModel.isSelect()) {
                        labelModel.setSelect(false);
                    } else {
                        for (LabelModel model : mData) {
                            model.setSelect(false);
                        }
                        labelModel.setSelect(true);
                    }
                }
                notifyDataSetChanged();
                onLabelItemClickListener.onLabelItemClickListener(flag, labelModel);
            });
        }
    }

    @NonNull
    @Override
    protected DataBindingHolder<ItemLabelBinding> onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup viewGroup, int i) {
        return new DataBindingHolder<>(R.layout.item_label, viewGroup);
    }

    public interface OnLabelItemClickListener {
        void onLabelItemClickListener(int flag, LabelModel model);
    }
}
